{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=53
  Verdict: Accepted
}
uses 
	SysUtils, Math;
 
var  	
	i,j,n,tt,kk,ls:longint;
	f:boolean;
	a:array of array of longint;
	t:array[0..1010] of longint;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n);
f:=true;
setlength(a,n+1);
for i:=1 to n do begin
	read(tt);
	setlength(a[i],tt+1);
	for j:=1 to tt do begin
		read(a[i][j]);
		if a[i][j]<>i then f:=false;
	end;
end;
if f then halt(0) else
if n=2 then begin
	f:=true;
	ls:=length(a[1])-1;
	tt:=0;
	for i:=1 to ls do begin
		inc(tt);
		t[tt]:=a[1][i];
	end;
	ls:=length(a[2])-1;
	for i:=ls downto 1 do begin
		inc(tt);
		t[tt]:=a[2][i];
	end;
	for i:=2 to tt do
		if (t[i-1]=2) and (t[i]=1) then begin
			f:=false;
			break;
		end;
	if f then begin
		kk:=length(a[2])-1;
		ls:=length(a[1]);  
		setlength(a[1],ls+kk);
		tt:=ls;
		for i:=kk downto 1 do begin
			writeln('2 1');
			a[1][tt]:=a[2][i];
			inc(tt);
		end;
		ls:=length(a[1])-1;
		while a[1][ls]=2 do begin
			writeln('1 2');
			dec(ls);
		end;
	end else write(0);
end else begin
	for i:=2 to n do begin
		ls:=length(a[i])-1;
		kk:=length(a[1]);
		setlength(a[1],kk+ls);
		tt:=kk;
		for j:=ls downto 1 do begin
			writeln(i,' 1');
			a[1][tt]:=a[i][j];
			inc(tt);
		end;
		setlength(a[i],1);
	end;
	ls:=length(a[1])-1;
	for i:=ls downto 1 do
		if a[1][i]=1 then begin
			kk:=length(a[n]);
			setlength(a[n],kk+1);
			a[n][kk]:=1;
			writeln('1 ',n);
		end else begin
			tt:=a[1][i];
			kk:=length(a[tt]);
			setlength(a[tt],kk+1);
			a[tt][kk]:=tt;
			writeln('1 ',tt);
		end;    
	setlength(a[1],1);
	ls:=length(a[n])-1;
	for i:=ls downto 1 do
		if a[n][i]=1 then begin
			kk:=length(a[1]);
			setlength(a[1],kk+1);
			a[1][kk]:=1;
			writeln(n,' 1');
		end else
		if a[n][i]=n then begin
			kk:=length(a[2]);
			setlength(a[2],kk+1);
			a[2][kk]:=n;
			writeln(n,' 2');
		end;
	setlength(a[n],1);
	ls:=length(a[2])-1;
	while a[2][ls]=n do begin
		writeln('2 ',n);
		dec(ls);
	end;
end;
end.
end.